package ru.acme.lab13.service.bank;

import ru.acme.lab13.domain.bank.Bank;
import ru.acme.lab13.domain.bank.ImmutableBank;
import ru.acme.lab13.domain.client.Client;
import ru.acme.lab13.domain.client.ClientRegistrationListener;
import ru.acme.lab13.domain.client.ImmutableClient;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Scanner;

public class BankService {
	private final static String bankBackup = "C:\\bankbackup.ser";
	
	public static void addClient(Bank bank, Client client) {
		bank.addClient(client);
	}
	
	public static void modifyBank(Bank bank) {
		for (Client client : bank.getClients()) {
			if (Math.random() > 0.5) {
				client.getAccounts().get(0).deposit((int)(100 * Math.random()));
			} else {
				client.getAccounts().get(0).withdraw((int)(100 * Math.random()));
			}
		}
		try {
			writeBank(bank, bankBackup);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void printBalances(ImmutableBank bank) {
		for (ImmutableClient client : bank.getImmutableClients()) {
			System.out.print(client.getImmutableAccounts().get(0).getBalance() + " ");
		}
		System.out.println();
	}

	public static void printMaximumAmountsToWithdraw(ImmutableBank bank) {
		for (ImmutableClient client : bank.getImmutableClients()) {
			System.out.print(client.getImmutableAccounts().get(0).maximumAmountToWihdraw() + " ");
		}
		System.out.println();
	}

	public static void addListener(Bank bank, ClientRegistrationListener listener) {
		bank.addListener(listener);
		try {
			writeBank(bank, bankBackup);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	public static void writeBank(Bank bank, String fileName) throws IOException {
		FileOutputStream stream = new FileOutputStream(fileName);
		try {
			writeBank(bank, stream);
		} catch (IOException e) {
			throw e;
		} finally {
			stream.close();			
		}
	}

	public static Bank readBank(String fileName) throws IOException, ClassNotFoundException {
		FileInputStream stream = new FileInputStream(fileName);
		Bank bank;
		try {
			bank = readBank(stream);
		} catch (IOException e) {
			throw e;
		} finally {
			stream.close();			
		}
		return bank;	
	}

	public static void writeBank(Bank bank, OutputStream stream) throws IOException {
		ObjectOutputStream objectStream = new ObjectOutputStream(stream);
		try {
			objectStream.writeObject(bank);
		} catch (IOException e) {
			throw e;
		} finally {
			objectStream.close();			
		}
	}

	public static Bank readBank(InputStream stream) throws IOException, ClassNotFoundException {
		ObjectInputStream objectStream = new ObjectInputStream(stream);
		Bank bank;
		try {
			bank = (Bank) objectStream.readObject();
		} catch (IOException e) {
			throw e;
		} finally {
			objectStream.close();			
		}
		return bank;
	}
	
	public static void loadClientsFromFile(Bank bank, String fileName) throws FileNotFoundException, ParseException {
		FileInputStream stream = new FileInputStream(fileName);
		Scanner scanner = new Scanner(stream);
		while (scanner.hasNext()) {
			String clientString = scanner.nextLine();
			addClient(bank, Client.parseClient(clientString));
		}	
	}
}
